package im.starchat.chat.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import im.starchat.chat.XLApplication;
import im.starchat.chat.rest.model.UserBean;

/**
 * Created by liuchao on 9/28/15.
 */
public class PreferencesStorageUtil {

    private static final String PREFERENCES_NAME = "cn_coloapp_android";

    private static final String IS_LAUNCH_FIRST = "is_first_launch" + XLApplication.sVersionName;

    private static final String USER_TOKEN = "user_token";

    private static final String USER_INFO_JSON = "user_info_json";

    private static final String IS_STAR = "is_star";

    private static final String KEY_DOWNLOAD_ID = "key_download_id";

    private static final String TSIG = "tsig";

    private static final String ACCOUNT_DIAMOND_COUNT = "diamond_count";

    private static final String ACCOUNT_DANMU_PRICE = "danmu_price";

    private static final String DEVICE_TOKEN = "device_token";

    private static final String DEBUG_MODE = "debug_mode";

    private static final String OFFICIAL_MODE = "official_mode";

    private static final String USER_ID = "user_id";


    public static boolean isFirstLaunch(Context context){
        if (null == context) {
            return true;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getBoolean(IS_LAUNCH_FIRST, true);
    }

    public static void setFirstLaunch(Context context){
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean(IS_LAUNCH_FIRST, false);
        editor.apply();
    }

    public static boolean isStar(Context context){
        if (null == context) {
            return true;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getBoolean(IS_STAR, false);
    }

    public static void setStar(Context context, boolean star){
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean(IS_STAR, star);
        editor.apply();
    }

    public static String getDeviceToken(Context context){
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(DEVICE_TOKEN, "");
    }

    public static void setDeviceToken(Context context, String deviceToken){
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(DEVICE_TOKEN, deviceToken);
        editor.apply();
    }

    public static boolean isUseOfficialUrl(Context context){
        if (null == context) {
            return true;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getBoolean(OFFICIAL_MODE, true);
    }

    public static void setUseOfficialUrl(Context context, boolean isUseOfficail){
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean(OFFICIAL_MODE, isUseOfficail);
        editor.apply();
    }

    public static int getDiamondCount(Context context){
        if (null == context) {
            return 0;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getInt(ACCOUNT_DIAMOND_COUNT, 0);
    }

    public static void setDiamondCount(Context context, int diamondCount){
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt(ACCOUNT_DIAMOND_COUNT, diamondCount);
        editor.apply();
    }

    public static int getDanmuPrice(Context context){
        if (null == context) {
            return Constants.DANMU_PRICE;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Constants.DANMU_PRICE);
        return pref.getInt(ACCOUNT_DANMU_PRICE, Constants.DANMU_PRICE);
    }

    public static void setDanmuPrice(Context context, int diamondCount){
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Constants.DANMU_PRICE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt(ACCOUNT_DANMU_PRICE, diamondCount);
        editor.apply();
    }



    public static boolean isUserLogin(Context context){
        if (null == context) {
            return true;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        String token = pref.getString(USER_TOKEN, "");
        if(TextUtils.isEmpty(token)){
            return false;
        }else{
            return true;
        }
    }

    public static String getUserId(Context context){
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(USER_ID, "");
    }

    public static void setUserid(Context context, String userId){
        if (null == context) {
            return ;
        }
        if(!TextUtils.isEmpty(userId)){
            SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.putString(USER_ID, userId);
            editor.apply();
        }
    }


    public static void putDownloadId(Context context, long id) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putLong(KEY_DOWNLOAD_ID, id);
        editor.commit();
    }

    public static long getDownloadId(Context context) {
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getLong(KEY_DOWNLOAD_ID, 0l);
    }

    public static String getTsig(Context context){
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(TSIG, "");
    }

    public static void setTsig(Context context, String token){
        if (null == context) {
            return ;
        }
        if(!TextUtils.isEmpty(token)){
            SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.putString(TSIG, token);
            editor.apply();
        }
    }

    public static String getToken(Context context){
        if (null == context) {
            return "";
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getString(USER_TOKEN, "");
    }

    public static void setToken(Context context, String token){
        if (null == context) {
            return ;
        }
        if(!TextUtils.isEmpty(token)){
            SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.putString(USER_TOKEN, token);
            editor.apply();
        }
    }

    public static boolean isDebugMode(Context context){
        if (null == context) {
            return false;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        return pref.getBoolean(DEBUG_MODE, false);
    }

    public static void setDebugMode(Context context, boolean debug_mode){
        if (null == context) {
            return ;
        }
            SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.putBoolean(DEBUG_MODE, true);
            editor.apply();
    }

    public static UserBean getUserBean(Context context){
        if (null == context) {
            return null;
        }
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        String userStr = pref.getString(USER_INFO_JSON, "");
        if(!TextUtils.isEmpty(userStr)){
            return GsonUtil.fromJson(userStr, UserBean.class);
        }else{
            return null;
        }
    }

    public static void setUserBean(Context context, String userJson){
        if (null == context) {
            return ;
        }
        if(!TextUtils.isEmpty(userJson)){
            SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = pref.edit();
            editor.putString(USER_INFO_JSON, userJson);
            editor.apply();
        }
    }

    public static void logout(Context context){
        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(USER_TOKEN, "");
        editor.putString(USER_INFO_JSON, "");
        editor.apply();
    }

//    public static String getUserName(Context context){
//        if (null == context) {
//            return "";
//        }
//        SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
//        return pref.getString(USER_NAME, "");
//    }
//
//    public static void setUserName(Context context, String userId){
//        if (null == context) {
//            return ;
//        }
//        if(!TextUtils.isEmpty(userId)){
//            SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
//            SharedPreferences.Editor editor = pref.edit();
//            editor.putString(USER_NAME, userId);
//            editor.apply();
//        }
//    }

}
